//
//  Expression.m
//  极致荟
//
//  Created by hetao on 14-6-17.
//  Copyright (c) 2014年 mac. All rights reserved.
//

#import "Expression.h"

@implementation Expression
UIPageControl *page;
JZHChatViewController *controller;
//表情界面初始化
+ (Expression *)shareExpression:(UITextField *)textview viewconncrerll:(JZHChatViewController *)con
{
    static Expression *expression;
    if(!expression)
    {
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            expression=[[super alloc]initWithFrame:CGRectMake(0, HEIGHT+20, 320, 180)];
            [expression setBackgroundColor:[UIColor whiteColor]];
            [[UIApplication sharedApplication].keyWindow addSubview:expression];
            [expression viewLoad];
        });
    }
    if(con)
    {
        expression.textview= textview;
        controller=con;
    }
    return expression;
}

///界面元素初始化
- (void)viewLoad
{
  
    UIScrollView *scrollview=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 320, self.frame.size.height-30)];
    [scrollview setContentSize:CGSizeMake(320*5, 0)];
    scrollview.pagingEnabled=YES;
    scrollview.showsVerticalScrollIndicator=NO;
    scrollview.delegate=self;
    
    //表情图片下标
  __block  int faceindex=0;
    //表情分为5组
    for(int i=0;i<5;i++)
    {
        UIView *view=[[UIView alloc]initWithFrame:CGRectMake(i*320, 0, 320, self.frame.size.height)];
        //每组表情为7*3
        for(int j=0;j<3;j++)
        {
            for(int k=0;k<7;k++)
            {
                UIButton *but=[[UIButton alloc]initWithFrame:CGRectMake((1+k)*8+k*36, (j+1)*8+j*36, 36, 36)];
              
                if((j+1)*(k+1)==21)
                {
                   
                    [but setImage:[UIImage imageNamed:@"shang"] forState:UIControlStateNormal];
                     [but addTarget:self action:@selector(shang:) forControlEvents:UIControlEventTouchUpInside];
                }else
                {
                    but.tag=220+faceindex;
                    [but addTarget:self action:@selector(showface:) forControlEvents:UIControlEventTouchUpInside];
                 
                    UIImage *img=[UIImage imageNamed:[NSString stringWithFormat:@"%df.gif",faceindex]] ;
                    [but setImage:img forState:UIControlStateNormal];
                                      faceindex++;

                }
                
                [view addSubview:but];
                
            }
        }
        
        [scrollview addSubview:view];
    }
    [self addSubview:scrollview];
    page=[[UIPageControl alloc]initWithFrame:CGRectMake(120, self.frame.size.height-10, 100, 5)];
    page.pageIndicatorTintColor=[UIColor grayColor];
    page.currentPageIndicatorTintColor=[UIColor blackColor];
    [page setNumberOfPages:5];
    [self addSubview:page];
    
    UIButton *but=[[UIButton alloc]initWithFrame:CGRectMake(0, 135, 320, 30)];
    [but setBackgroundColor:[UIColor blueColor]];
    
    [but setTitle:@"发送" forState:UIControlStateNormal];
    [but addTarget:self action:@selector(senderMess:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:but];
}

- (void)senderMess:(UIButton *)but
{
    if(![self.textview.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]].length==0)
    {
        [controller senderMessge:self.textview.text tyep:TEXT];
    }
    [self hidden];
    self.textview.text=@"";
    controller.expression.selected=NO;
    [controller aoutTextViewFram];
    [UIView animateWithDuration:.3 animations:^{
        [controller.view setCenter:CGPointMake(160, [UIApplication sharedApplication].keyWindow.frame.size.height/2)];
    }];
}

- (void)showface:(UIButton*)sender
{
    self.textview.text=[NSString stringWithFormat:@"%@/f%d;",self.textview.text,sender.tag-220];
    NSLog(@"%@",self.textview.text);
    [controller aoutTextViewFram];
    
}
- (void)shang:(UIButton*)sender
{

    if(self.textview.text.length<1)
    {
        return;
    }
   NSMutableArray* arr= [[NSMutableArray alloc]initWithArray:[self.textview.text componentsSeparatedByString:@"/f"]];
    NSLog(@"%d   %@",arr.count,arr);
    NSArray *arr1;
    for(NSString *str in arr)
    {
        NSLog(@"%@",[str componentsSeparatedByString:@";"]);
        arr1=[str componentsSeparatedByString:@";"];
    }
    //得到最后一组分割的字符串
    NSString *charstr=[arr1 objectAtIndex:arr1.count-1];
    if(arr1.count==2 && charstr.length<1)
    {
        [arr removeObjectAtIndex:arr.count-1];
        charstr=@"";
        int i=0;
        for(NSString *str in arr)
        {
            if(i!=0)
            {
                charstr=  [charstr stringByAppendingString:[NSString stringWithFormat:@"/f%@",str ]];
            }
            else
            {
                charstr=  [charstr stringByAppendingString:str];
            }
            i++;
        }

        self.textview.text=charstr;
    }else
    {
       
        self.textview.text=[ self.textview.text substringToIndex: self.textview.text.length-1];
    }
     [controller aoutTextViewFram];

}

///显示表情键盘
- (void)show
{
 
    [UIView animateWithDuration:0.35 animations:^{
        [self setFrame:CGRectMake(0, HEIGHT+20-self.frame.size.height, self.frame.size.width, self.frame.size.height)];
        [self setHidden:NO];
    }];
    
}

///隐藏表情键盘
- (void)hidden
{
   
    [UIView animateWithDuration:0.35 animations:^{
        [self setFrame:CGRectMake(0, HEIGHT+20, self.frame.size.width, self.frame.size.height)];
    } completion:^(BOOL finished) {
        [self setHidden:YES];
    }];
}
#pragma mark----scorllview delete-----
- (void)scrollViewDidScroll:(UIScrollView *)scrollView;
{
    
    CGFloat x=scrollView.contentOffset.x/320;
    if(fmod (x,1 )==0)
    {
        [page setCurrentPage:x];
    }
    
}
@end
